﻿#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

#define B_SIZE 60
#define B_NUM 8
#define LEVEL_TOTALNUM 5

struct Player
{
	int i;
	int j;
};

Player player;

char levels[LEVEL_TOTALNUM][B_NUM][B_NUM + 1] =
{
	{ "wwwwwwww", "wwwtbeew" ,"weeeeeew", "weeeeeew", "weeeeeew", "weepaeew", "weeewwww", "wwwwwwww" },//Level 1.
	{ "wwwwwwww", "wweeewww", "wpetbwww", "weeebeww", "wewteeww", "weeeeeww", "weepwwww", "wwwwwwww" },//Level 2.
	{ "wwwwwwww", "wwpeewww", "weeweeww", "webabeww", "weeteeww", "wwetewww", "wwwwwwww", "wwwwwwww" },//Level 3.
	{ "wwwwwwww", "wwwwwwww", "weeeewww", "weeettew", "webbbpew", "weewetww", "wwwwwwww", "wwwwwwww" },//Level 4.
	{ "wwwwwwww", "wwwwwwww", "wwteewww", "weewebpw", "weewewew", "weaeebtw", "weeeewww", "wwwwwwww" } //Level 5.
};

int current_level_num = 0;
char level[B_NUM][B_NUM + 1];
int target_num;
int achieve_num;

void startup()
{
	initgraph(B_NUM * B_SIZE, B_NUM * B_SIZE);
	setbkcolor(LIGHTGRAY);
	BeginBatchDraw();

	for (int i = 0; i < B_NUM; i++)
	{
		for (int j = 0; j < B_NUM; j++)
		{
			level[i][j] = levels[current_level_num][i][j];
		}
	}

	target_num = 0;
	achieve_num = 0;

	for (int i = 0; i < B_NUM; i++)
	{
		for (int j = 0; j < B_NUM; j++)
		{
			if (level[i][j] == 'p')
			{
				player.i = i;
				player.j = j;
				level[i][j] = 'e';
			}
			else if (level[i][j] == 't' || level[i][j] == 'a')
			{
				target_num++;
			}
		}
	}
}


void show()
{
	int i;
	int j;
	cleardevice();

	for (i = 0; i < B_NUM; i++)
	{
		for (j = 0; j < B_NUM; j++)
		{
			if (level[i][j] == 'e')
			{
				setfillcolor(LIGHTGRAY);
				setlinecolor(LIGHTGRAY);
				fillrectangle(j * B_SIZE, i * B_SIZE, (j + 1) * B_SIZE, (i + 1) * B_SIZE);
			}
			else if (level[i][j] == 'w')
			{
				setfillcolor(LIGHTRED);
				setlinecolor(LIGHTGRAY);
				fillrectangle(j * B_SIZE, i * B_SIZE, (j + 1) * B_SIZE, (i + 1) * B_SIZE);
			}
			else if (level[i][j] == 'b')
			{
				setfillcolor(YELLOW);
				setlinecolor(LIGHTGRAY);
				fillrectangle(j * B_SIZE, i * B_SIZE, (j + 1) * B_SIZE, (i + 1) * B_SIZE);
			}
			else if (level[i][j] == 't')
			{
				setfillcolor(WHITE);
				fillrectangle((j + 0.3) * B_SIZE, (i + 0.3) * B_SIZE, (j + 0.7) * B_SIZE, (i + 0.7) * B_SIZE);
			}
			else if (level[i][j] == 'a')
			{
				setlinecolor(LIGHTGRAY);
				setfillcolor(YELLOW);
				fillrectangle(j * B_SIZE, i * B_SIZE, (j + 1) * B_SIZE, (i + 1) * B_SIZE);
				setfillcolor(WHITE);
				fillrectangle((j + 0.3) * B_SIZE, (i + 0.3) * B_SIZE, (j + 0.7) * B_SIZE, (i + 0.7) * B_SIZE);
			}

		}
	}

	i = player.i;
	j = player.j;

	setfillcolor(RED);
	fillcircle((j + 0.5) * B_SIZE, (i + 0.5) * B_SIZE, 0.4 * B_SIZE);

	setfillcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle((j + 0.3) * B_SIZE, (i + 0.45) * B_SIZE, 0.08 * B_SIZE);
	fillcircle((j + 0.7) * B_SIZE, (i + 0.45) * B_SIZE, 0.08 * B_SIZE);

	setlinestyle(PS_SOLID, 3);
	line((j + 0.35) * B_SIZE, (i + 0.7) * B_SIZE, (j + 0.65) * B_SIZE, (i + 0.7) * B_SIZE);
	setlinestyle(PS_SOLID, 1);

	if (achieve_num == target_num)
	{
		setbkmode(TRANSPARENT);
		settextcolor(RGB(0, 255, 100));
		TCHAR text[20];
		if (current_level_num < LEVEL_TOTALNUM - 1)
		{
			settextstyle(50, 0, _T("楷体"));
			swprintf_s(text, _T("开始第%d关"), current_level_num + 2);
			outtextxy(120, 160, text);
			outtextxy(10, 250, _T("按空格键重玩当前关"));
		}
		else
		{
			settextstyle(80, 0, _T("楷体"));
			outtextxy(80, 200, _T("游戏胜利"));
		}
		FlushBatchDraw();
		Sleep(2500);
	}
	FlushBatchDraw();
}


void update()
{
	if (_kbhit() && (achieve_num < target_num))
	{
		char input = _getch();
		if (input == ' ')
		{
			startup();
		}
		if (input == 'a' || input == 's' || input == 'd' || input == 'w')
		{
			int goal_i = player.i;
			int goal_j = player.j;
			int goal_next_i = goal_i;
			int goal_next_j = goal_j;

			if (input == 'a')
			{
				goal_j = player.j - 1;
				goal_next_j = goal_j - 1;
			}
			else if (input == 'd')
			{
				goal_j = player.j + 1;
				goal_next_j = goal_j + 1;
			}
			else if (input == 's')
			{
				goal_i = player.i + 1;
				goal_next_i = goal_i + 1;
			}
			else if (input == 'w')
			{
				goal_i = player.i - 1;
				goal_next_i = goal_i - 1;
			}

			if (level[goal_i][goal_j] == 'e' || level[goal_i][goal_j] == 't')
			{
				player.i = goal_i;
				player.j = goal_j;
			}
			else if (level[goal_i][goal_j] == 'b' && level[goal_next_i][goal_next_j] == 'e')
			{
				player.i = goal_i;
				player.j = goal_j;
				level[goal_i][goal_j] = 'e';
				level[goal_next_i][goal_next_j] = 'b';
			}
			else if (level[goal_i][goal_j] == 'b' && level[goal_next_i][goal_next_j] == 't')
			{
				player.i = goal_i;
				player.j = goal_j;
				level[goal_i][goal_j] = 'e';
				level[goal_next_i][goal_next_j] = 'a';
			}
			else if (level[goal_i][goal_j] == 'a' && level[goal_next_i][goal_next_j] == 'e')
			{
				player.i = goal_i;
				player.j = goal_j;
				level[goal_i][goal_j] = 't';
				level[goal_next_i][goal_next_j] = 'b';
			}
			else if (level[goal_i][goal_j] == 'a' && level[goal_next_i][goal_next_j] == 't')
			{
				player.i = goal_i;
				player.j = goal_j;
				level[goal_i][goal_j] = 't';
				level[goal_next_i][goal_next_j] = 'a';
			}
			else
			{
				return;
			}
		}

		achieve_num = 0;

		for (int i = 0; i < B_NUM; i++)
		{
			for (int j = 0; j < B_NUM; j++)
			{
				if (level[i][j] == 'a')
				{
					achieve_num++;
				}
			}
		}
		if (achieve_num == target_num)
		{
			show();
			if (current_level_num < LEVEL_TOTALNUM - 1)
			{
				current_level_num++;
				startup();
			}
		}
	}
}


int main()
{
	startup();
	while (1)
	{
		show();
		update();
	}

	return 0;
}